package core.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SerializeUtil {
	private static Logger logger = LoggerFactory.getLogger(SerializeUtil.class);
	
	public static byte[] serialize(Object object) throws IOException {
		ObjectOutputStream oos = null;
		ByteArrayOutputStream baos = null;
		baos = new ByteArrayOutputStream();
		oos = new ObjectOutputStream(baos);
		oos.writeObject(object);
		return baos.toByteArray();
	}

	public static Object deserialize(byte[] bytes) throws IOException {
		if (bytes == null) {
			return null;
		}
		ByteArrayInputStream bais = null;
		bais = new ByteArrayInputStream(bytes);
		ObjectInputStream ois = new ObjectInputStream(bais);
		try {
			return ois.readObject();
		} catch (ClassNotFoundException e) {
			logger.error("ClassNotFoundException", e);
			throw new IOException(e);
		}
	}
}